<template>
  <div class="comment-item" :style="{ marginLeft: depth * 30 + 'px' }">
    <el-avatar :size="36" :src="comment.avatar" />
    <div class="comment-main">
      <div class="header-row">
        <span class="comment-author">{{ comment.nickname }}</span>
      </div>
      <div class="comment-text">{{ comment.content }}</div>
      <div class="comment-footer">
        <span class="comment-time">{{ comment.createdAt }}</span>
        <div class="comment-actions">
          <el-icon><Star /></el-icon>
          <span>{{ comment.likes }}</span>
          <el-icon><ChatDotRound /></el-icon>
          <span @click="handleReplyClick">回复</span>
          <!-- 删除按钮，仅作者可见 -->
          <template v-if="props.currentUserId === comment.userId">
            <el-dropdown trigger="click">
              <span class="el-dropdown-link">
                <el-icon><MoreFilled /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleDelete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </div>
      </div>
      <!-- 回复输入框 (当点击回复时显示) -->
      <div v-if="showReplyInput" class="reply-input">
        <el-input
          v-model="replyContent"
          placeholder="写下你的回复"
          size="large"
          :autosize="{ minRows: 2, maxRows: 4 }"
        />
        <div class="reply-buttons">
          <el-button size="large" @click="cancelReply">取消</el-button>
          <el-button type="primary" size="large" @click="submitReply">回复</el-button>
        </div>
      </div>
      <!-- 递归渲染子评论 -->
      <div v-if="hasChildren" class="child-comments">
        <comment-item
          v-for="child in children"
          :key="child.id"
          :comment="child"
          :all-comments="allComments"
          :depth="depth + 1"
          :current-user-id="props.currentUserId"
          @reply="handleChildReply"
          @delete-comment="emit('delete-comment', $event)"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessageBox } from 'element-plus'

const props = defineProps({
  comment: Object,
  allComments: Array,
  depth: Number,
  currentUserId: Number
})

const emit = defineEmits(['reply', 'delete-comment'])

const showReplyInput = ref(false)
const replyContent = ref('')

// 计算当前评论的子评论
const children = computed(() => {
  return props.allComments.filter(c => c.parentId === props.comment.id)
})

const hasChildren = computed(() => {
  return children.value.length > 0
})

const handleReplyClick = () => {
  showReplyInput.value = true
}

const cancelReply = () => {
  showReplyInput.value = false
  replyContent.value = ''
}

const submitReply = () => {
  if (replyContent.value.trim()) {
    emit('reply', {
      parentId: props.comment.id,
      content: replyContent.value
    })
    showReplyInput.value = false
    replyContent.value = ''
  }
}

const handleChildReply = (data) => {
  emit('reply', data)
}

const handleDelete = async () => {
  try {
    await ElMessageBox.confirm('确定要删除这条评论吗？', '提示', {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    })
    emit('delete-comment', props.comment.id)
  } catch (e) {}
}

</script>

<style scoped>
.comment-item {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  padding: 18px 0 8px 0;
  border-bottom: 1px solid #f1f2f3;
  gap: 12px;
}
.comment-main {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.header-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 2px;
}
.comment-author {
  font-weight: 600;
  font-size: 14px;
  color: #18191c;
  margin-left: 2px;
  margin-top: 2px;
}
.comment-text {
  font-size: 15px;
  color: #222;
  line-height: 1.7;
  margin-bottom: 10px;
  word-break: break-word;
}
.comment-footer {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}
.comment-time {
  font-size: 12px;
  color: #9499a0;
  margin-right: 10px;
}
.comment-actions {
  display: flex;
  align-items: center;
  gap: 18px;
  font-size: 13px;
  color: #9499a0;
}
.comment-actions span,
.comment-actions .el-icon {
  cursor: pointer;
  transition: color 0.2s;
}
.comment-actions span:hover,
.comment-actions .el-icon:hover {
  color: #00aeec;
}
.reply-input {
  margin-top: 16px;
  width: 100%;
}
.reply-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
  gap: 12px;
}
.child-comments {
  border-left: 2px solid #e5e7eb;
  padding-left: 18px;
  margin-top: 18px;
}
</style>
